% -----------------------
% Project: Secret and Overt Information Acquisition in Financial Markets
% This code produces Panel a of Figure 3
% Site visit: Monopoly case
% -----------------------
clear;clc;
J=1;
c=0.2;Su=1;
options = optimset('Display','off', 'TolFun', 1e-15,'TolX',1e-15); % Turn off Display
tolfval=1e-10;

HHbar=linspace(0,1.5,50);
Hvol=NaN(size(HHbar));%h voluntary
Hmand1=NaN(size(HHbar));%h mandatory
Hmand2=NaN(size(HHbar));%h mandatory

   % Solve for ho and hs
   % overt: ho
    fun = @(h) ( (h*(1+h)*(2+h)^2*(2+h+h*J)^4)/...
                 ((6*J^2-J-3)*h^2+(4*J^2+10*J-8)*h+8*J-4 )^2- Su^2/(4*J^3*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [ho,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);      
   % secret: hs
    fun = @(h) ( h*(1+h)*(2+h+h*J)^2 - Su^2/(J*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [hs,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
    param=[J,c,Su];
    
    % Solve for eta
    fun = @(h) ( pij(hs,hs,hs,param)-pij(h,h,h,param) )^2;
    h0 = ho-0.05; A = [1]; bb = [ho];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [eta,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options); 

% Solve for h*   
for i=1:length(HHbar)
    hbar=HHbar(i);
   %================================ 
   % 1. Mandatory : Monopoly
    if hbar<=hs
        hcan1=hbar-1e-6;
        pieqm1=pij(hcan1,hcan1,hcan1,param);
        pidev=pij(hs,hs,hs,param);
        if pieqm1>=pidev
            Hmand1(i)=hcan1;
        else
            Hmand2(i)=hs;
        end
    else
        pieqm1=pij(hs,hs,hs,param);
        pidev=pij(hbar,hbar,hbar,param);
        if pieqm1>=pidev
            Hmand2(i)=hs;
        end
    end
    
   %================================    
    % 2. Voluntary : Monopoly
    Hvol(i)=hs;
end


figure(10);
plot(HHbar,Hvol,'b-',HHbar,Hmand1,'r--o',HHbar,Hmand2,'r--o','LineWidth',1);hold on;
legend('Voluntary','Mandatory','Interpreter','latex', 'AutoUpdate', 'off'); legend box off;
plot(HHbar,Hvol,'b-','LineWidth',1.8);hold off;
ylim([0,HHbar(end)]);
xlabel('$\bar{h}$','Interpreter','latex'); 
ylabel('$h^*$','Interpreter','latex');
   box off; set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');
set(gcf, 'Position',  [100, -100, 670, 620]); 

x1 = xline(hs,'-.','$h_s$','DisplayName','hs','Interpreter','latex');
set(x1,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x1,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
x2 = xline(ho,'-.','$h_o$','DisplayName','ho','Interpreter','latex');
set(x2,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x2,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
x3 = xline(eta,'-.','$\eta$','DisplayName','eta','Interpreter','latex');
set(x3,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x3,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
text(-0.1,0.9,'$h_s$',...
    'Interpreter','latex','fontsize', 22);





